<template>
    
    <div class="dashboard-container">
        <div id="container">
            <el-form :inline="true" align="center">
                <el-form-item label="名称"><el-input v-model="name" placeholder="请输入名称"></el-input></el-form-item>
                <el-form-item label="性别"> <el-select v-model="sex" placeholder="请选择性别" clearable>
                        <el-option label="男" value="男"></el-option>
                        <el-option label="女" value="女"></el-option>
                    </el-select></el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="pageQuery()">查询</el-button>
                <el-button type="primary" icon="el-icon-close" @click="reset()">重置</el-button>
                <el-button type="primary" icon="el-icon-plus el-icon-plus" @click="dialogFormVisible2=true">新增</el-button>
            </el-form>
        </div>

        <el-table :data="tableDate" stripe style="width: 100% " @selection-change="handleSelectionChange">
            <el-table-column prop="id" label="id" align="center"></el-table-column>
            <el-table-column prop="no" label="账号" align="center"></el-table-column>
            <el-table-column prop="name" label="名称" align="center"> </el-table-column>
            <el-table-column prop="age" label="年龄" align="center"> </el-table-column>
            <el-table-column prop="sex" label="性别" align="center"></el-table-column>
            <el-table-column prop="phone" align="center" label="电话"></el-table-column>
            <el-table-column align="center" label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" size="small" icon=" el-icon-edit" @click="sel(scope.row.id),dialogFormVisible3=true">编辑</el-button>
                    <el-button type="primary" size="small" icon=" el-icon-delete" @click="del(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination align="center" @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="page" :background="background" :page-sizes="[5, 10, 15, 20]" :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
        </el-pagination>

        <!-- 点击新增按钮弹出界面 -->
       <div class="background-color">
        <el-dialog title="添加用户" :visible.sync="dialogFormVisible2">
            <el-form :model="user" :rules="rules" ref="addForm">
                <el-form-item label="账号" :label-width="formLabelWidth" prop="no">
                    <el-input v-model="user.no" class="size"></el-input>
                    <label style="margin-left: 90px;">姓名：</label>
                    <el-input v-model="user.name" class="size" prop="name"></el-input>
                </el-form-item>

                <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
                    <el-input v-model.number="user.age" class="size"></el-input>
                    <label style="margin-left: 90px;">性别：</label>
                    <el-select v-model="user.sex" placeholder="请选择" prop="sex" class="size">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>                    
                </el-form-item>

                <el-form-item label="电话号码：" :label-width="formLabelWidth" prop="phone">
                    <el-input v-model="user.phone" class="size"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible2 = false">取 消</el-button>
                <el-button type="primary" @click="submitAddForm">确 定</el-button>
            </div>
        </el-dialog>
    </div>

        <!-- 点击修改按钮弹出界面 -->
        <div class="background-color">
        <el-dialog title="修改" :visible.sync="dialogFormVisible3">
            <el-form :model="user" :rules="rules" ref="editForm">
                <el-form-item label="账号" :label-width="formLabelWidth" prop="no">
                    <el-input v-model="user.no" class="size"></el-input>
                    <label style="margin-left: 90px;">名称：</label>
                    <el-input v-model="user.name" class="size" prop="name"></el-input>
                </el-form-item>

                <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
                    <el-input v-model.number="user.age" class="size"></el-input>
                    <label style="margin-left: 90px;">性别：</label>
                    <el-select v-model="user.sex" placeholder="请选择" prop="sex" class="size">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>                    
                </el-form-item>
                
                <el-form-item label="电话号码：" :label-width="formLabelWidth" prop="phone">
                    <el-input v-model="user.phone" class="size"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible3 = false">取 消</el-button>
                <el-button type="primary" @click="submitEditForm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
    </div>

</template>

<script>
import {userPage,getById,selectById,addUser,update} from '@/api/UserManage'
export default {
    data() {
        // 验证手机号的函数
        const validatePhone = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('请输入电话号码'));
            }
            const reg = /^1[3-9]\d{9}$/;
            if (!reg.test(value)) {
                callback(new Error('请输入正确的手机号码'));
            } else {
                callback();
            }
        };
        return {
            id: null,
            no: null,
            name: null,
            password: null,
            sex: null,
            roleId: null,
            phone: null,
            isvalid: null,
            page: 1,
            pageSize: 10,

            tableDate:[],
            // 表单验证规则
            rules: {
                no: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                ],
                age: [
                    { required: true, message: '请输入年龄', trigger: 'blur' },
                    { type: 'number', message: '年龄必须为数字值', trigger: 'blur' },
                    { validator: (rule, value, callback) => {
                        if (value < 0 || value > 120) {
                            callback(new Error('请输入合理的年龄'));
                        } else {
                            callback();
                        }
                    }, trigger: 'blur' }
                ],
                sex: [
                    { required: true, message: '请选择性别', trigger: 'change' }
                ],
                phone: [
                    { required: true, validator: validatePhone, trigger: 'blur' }
                ]
            
        },
            options: [{
                    value: '男',
                    label: '男'
                }, {
                    value: '女',
                    label: '女'
                } ],
            user:{
            id: null,
            no: null,
            name: null,
            password: null,
            sex: null,
            roleId: null,
            phone: null,
            isvalid: null,
            },
            
            total: 0,
            dialogTableVisible: false,
            dialogFormVisible: false,
            dialogFormVisible2:false,
            dialogFormVisible3:false,
            formLabelWidth: '120px'
        }
    },

    //服务启动后自动执行分页查询
    created() {
        this.pageQuery();
    },

    methods: {
        //分页查询
        pageQuery() {
            const params = {
                id: this.id,
                no: this.no,
                name: this.name,
                password: this.password,
                sex: this.sex,
                roleId: this.roleId,
                phone: this.phone,
                isvalid: this.isvalid,
                page: this.page,
                pageSize: this.pageSize
            }
            userPage(params).then(res => {
                this.total = res.data.data.total
                this.tableDate = res.data.data.records
            })
        },

        //添加方法
        submitAddForm() {
            this.$refs.addForm.validate((valid) => {
                if (valid) {
                    addUser(this.user).then(res => {
                        this.$message.success("恭喜你，添加成功");
                        this.$router.push('/UserManage/UserManage');
                        this.dialogFormVisible2 = false;
                        this.pageQuery();
                    }).catch(error => {
                        this.$message.error("添加失败：" + error.message);
                    });
                } else {
                    this.$message.error('请填写正确的表单信息');
                    return false;
                }
            });
        },
        //重置
        reset() {
           // 重置所有查询条件
            this.id = null;
            this.no = null;
            this.name = null;
            this.password = null;
            this.sex = null;
            this.roleId = null;
            this.phone = null;
            this.isvalid = null;
            
            // 重置分页参数
            this.page = 1;
            this.pageSize = 10;
            
            // 重新查询
            this.pageQuery();
        },

        //删除
        del(id) {
            this.$confirm(`此操作将永久删除ID=${id}的数据, 是否继续?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                getById(id)
                    .then(res => {
                        if (res.data.code === 1) {
                            this.$message({
                                type: 'error',
                                message: '删除失败!'
                            })
                        } else {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            })
                            this.pageQuery()
                        }
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },

        //修改
        sel(id) {
           selectById(id).then(res=>{
            this.user=res.data.data
           })
        },
        submitEditForm() {
            this.$refs.editForm.validate((valid) => {
                if (valid) {
                    update(this.user).then(res => {
                        this.$message.success("修改成功");
                        this.dialogFormVisible3 = false;
                        this.pageQuery();
                        this.$router.push('/UserManage/UserManage');
                    }).catch(error => {
                        this.$message.error("修改失败：" + error.message);
                    });
                } else {
                    this.$message.error('请填写正确的表单信息');
                    return false;
                }
            });
        }
        },

        handleSizeChange(val) {
            this.pageSize = val
            this.pageQuery()
        },
        handleCurrentChange(val) {
            this.page = val
            this.pageQuery()
        },
        // 监控多选框事件
        handleSelectionChange(selection) {
            this.selectNum = selection.length;
            this.selection = selection;
        }
    }


</script>

<style scoped>
.size {
    height: 100%;
    width: 35%;
}
</style>